package com.payease.ezt.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * Created by wuyong on 16/8/5.
 */
public class NetStateReceiver extends BroadcastReceiver {
    private NetworkStateListener netWorkStateListener;
    @Override
    public void onReceive(Context context, Intent arg1) {
        ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        if (networkInfo != null){
            if (netWorkStateListener != null){
                netWorkStateListener.onStateChange(networkInfo.getState());
            }
        }else{
            netWorkStateListener.onStateChange(NetworkInfo.State.DISCONNECTED);
        }
    }

    public void setNetWorkStateListener(NetworkStateListener netWorkStateListener) {
        this.netWorkStateListener = netWorkStateListener;
    }


    public interface NetworkStateListener {
        void onStateChange(NetworkInfo.State state);
    }
}
